<?php

namespace backend\controllers;

use backend\models\ChangePassForm;
use backend\models\SignupForm;
use common\logic\AdminUserLogic;
use common\logic\RoleLogic;
use common\models\AuthRoleModel;
use common\server\BasicDataServer;

use Yii;
use yii\helpers\ArrayHelper;
use common\models\AdminModel;
use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;

/**
 * AdminUserController implements the CRUD actions for AdminModel model.
 */
class AdminUserController extends BaseController
{
    /**
     * Lists all AdminModel models.
     * @return mixed
     */
    public function actionIndex()
    {
    	$query = AdminUserLogic::instance()->getQuery(['ph_admin.status' => AdminModel::STATUS_ACTIVE],[],[],['ph_admin.id'=>SORT_DESC]);
    	$query->joinWith(['authRole']);
    	$query->select("ph_admin.*,ph_auth_role.name");
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        $dataProvider->sort = false;
        
		$roleArr = ArrayHelper::toArray(BasicDataServer::instance()->getRoleToOptions());
        
        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'roleArr'=>$roleArr
        ]);
    }

    /**
     * Displays a single AdminModel model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new AdminModel model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
    	
    	
        $model = new SignupForm();
        if ($model->load(Yii::$app->request->post())) {
        	
//      	 var_dump(Yii::$app->request->post());
//      	 var_dump($model);
//      	 exit();
        	
            if ($user = $model->signup()) {

                    return $this->redirect(['index']);

            }
        }

        return $this->render('create', [
            'model' => $model,
        ]);


    }

    /**
     * Updates an existing AdminModel model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $post = Yii::$app->request->post();
        if(!empty($post['AdminModel']['password_hash']))
        {
            $model->setPassword($post['AdminModel']['password_hash']);
            $model->nickname=$post['AdminModel']['nickname'];
            $model->role_id = $post['AdminModel']['role_id'];
            if($model->save()){
                return $this->redirect(['index']);
            }
        }
        else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing AdminModel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
    	
        AdminUserLogic::instance()->delete($id);

        return $this->redirect(['index']);
    }

    public function actionChangepass(){
        $model = new ChangePassForm();
        if ($model->load(Yii::$app->request->post()) && $model->change()) {
            return $this->goBack();
        } else {
            return $this->render('changepass', [
                'model' => $model,
            ]);
        }
    }
    

    
    /**
     * Finds the AdminModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return AdminModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = AdminModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
}
